package inline;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Pair;
import android.webkit.URLUtil;

import com.bytedance.frameworks.core.encrypt.TTEncryptUtils;
import com.bytedance.framwork.core.monitor.HttpResponseException;
import com.bytedance.framwork.core.monitor.MonitorNetUtil$CompressType;
import com.bytedance.framwork.core.monitor.MonitorNetUtil$NetworkType;
import com.ss.android.lockscreen.utils.NetworkUtils$NetworkType;
import com.ss.android.lockscreen.utils.b;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;
import java.util.zip.Deflater;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class NetworkDemo {
    public static boolean c(Context arg2) {
        if (arg2 == null) {
            boolean v0 = false;
            return v0;
        }

        try {
            NetworkInfo v0_2 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_2 != null) {
                if (!v0_2.isConnected()) {
                    return false;
                }

                return true;
            }

            return false;
        } catch (Exception v0_1) {
            v0_1.printStackTrace();
            return false;
        }
    }

    public static String d(Context arg3) {
        String v0;
        String v1 = null;
        if (arg3 == null) {
            v0 = v1;
            return v0;
        }

        try {
            NetworkInfo v0_2 = ((ConnectivityManager) arg3.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_2 != null && (v0_2.isAvailable())) {
                return v0_2.getTypeName();
            }

            v0 = v1;
        } catch (Throwable v0_1) {
            v0 = v1;
        }

        return v0;
    }

    public static boolean a(char arg1) {
        boolean v0 = arg1 == 32 || arg1 == 9 || arg1 == 13 || arg1 == 10 ? true : false;
        return v0;
    }

    public static String urlEncode(String input, String charset) {
        try {
            return URLEncoder.encode(input, charset);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static String a(String arg10, File arg11, String arg12) throws Throwable {
        byte[] v1_4;
        String v0_2;
        int v1_2;
        File v2_2;
        DataOutputStream v6;
        URLConnection v0_1;
        File v1 = null;
        if (!TextUtils.isEmpty(((CharSequence) arg10)) && arg11 != null && (arg11.exists())) {
            String v3 = "tt_file_upload" + UUID.randomUUID().toString();
            String v4 = "--";
            String v5 = "\r\n";
            String v2 = "multipart/form-data";
            try {
                v0_1 = new URL(arg10).openConnection();
                ((HttpURLConnection) v0_1).setReadTimeout(30000);
                ((HttpURLConnection) v0_1).setConnectTimeout(30000);
                ((HttpURLConnection) v0_1).setDoInput(true);
                ((HttpURLConnection) v0_1).setDoOutput(true);
                ((HttpURLConnection) v0_1).setUseCaches(false);
                ((HttpURLConnection) v0_1).setRequestMethod("POST");
                ((HttpURLConnection) v0_1).setRequestProperty("Charset", arg12);
                ((HttpURLConnection) v0_1).setRequestProperty("connection", "keep-alive");
                ((HttpURLConnection) v0_1).setRequestProperty("Content-Type", v2 + ";boundary=" + v3);
                ((HttpURLConnection) v0_1).setRequestProperty("Content-Encoding", "gzip");
                v6 = new DataOutputStream(((HttpURLConnection) v0_1).getOutputStream());
                if (arg11 != null) {
                    StringBuffer v2_1 = new StringBuffer();
                    v2_1.append(v4);
                    v2_1.append(v3);
                    v2_1.append(v5);
                    v2_1.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + arg11.getName() + ".zip\"" + v5);
                    v2_1.append("Content-Type: application/octet-stream; charset=" + arg12 + v5);
                    v2_1.append(v5);
                    v6.write(v2_1.toString().getBytes());
                    v2_2 = new File(arg11.getAbsolutePath() + "_tmp.zip");
                    if (v2_2.exists()) {
                        v2_2.delete();
                    }
                    a(arg11, v2_2);
                    FileInputStream v1_1 = new FileInputStream(v2_2);
                    byte[] v7 = new byte[1024];
                    while (true) {
                        int v8 = ((InputStream) v1_1).read(v7);
                        if (v8 == -1) {
                            break;
                        }
                        v6.write(v7, 0, v8);
                    }
                    ((InputStream) v1_1).close();
                    v6.write(v5.getBytes());
                    v6.write((v4 + v3 + v4 + v5).getBytes());
                    v6.flush();
                    v6.close();
                    v1_2 = ((HttpURLConnection) v0_1).getResponseCode();
                    if (v1_2 != 200) {
                        throw new HttpResponseException(v1_2, ((HttpURLConnection) v0_1).getResponseMessage());
                    }

                    InputStream v3_1 = ((HttpURLConnection) v0_1).getInputStream();
                    v0_2 = ((HttpURLConnection) v0_1).getContentEncoding();
                    if ((TextUtils.isEmpty(((CharSequence) v0_2))) || !v0_2.equalsIgnoreCase("gzip")) {
                        v1_4 = FileUtils.b(v3_1);
                    } else {
                        GZIPInputStream v1_3 = new GZIPInputStream(v3_1);
                        byte[] v0_3 = FileUtils.b(((InputStream) v1_3));
                        v1_3.close();
                        v1_4 = v0_3;
                    }

                    if (v3_1 != null) {
                        v3_1.close();
                    }
                    v0_2 = new String(v1_4);

                    if (!v2_2.exists()) {
                        return v0_2;
                    }
                    v2_2.delete();
                    return v0_2;
                } else {
                    return null;
                }
            } catch (Throwable v0) {
                v0.printStackTrace();
            }

            if (v1 != null && (v1.exists())) {
                v1.delete();
            }
            return null;
        }

        throw new IllegalArgumentException("url and file not be null ");
    }

    private static void a(File arg6, File arg7) throws IOException {
        int v0_1;
        FileOutputStream v3_1;
        FileInputStream v3 = null;
        GZIPOutputStream v1 = null;
        FileInputStream v4;
        FileOutputStream v2 = null;
        try {
            v4 = new FileInputStream(arg6);
            v3_1 = new FileOutputStream(arg7);
            v1 = new GZIPOutputStream(((OutputStream) v3_1));
            v0_1 = 1024;
            byte[] v0_2 = new byte[v0_1];
            while (true) {
                int v2_1 = v4.read(v0_2);
                if (v2_1 == -1) {
                    break;
                }
                v1.write(v0_2, 0, v2_1);
            }
        } catch (Throwable v0) {

        } finally {
            if (v1 != null) {
                v1.close();
            }

            if (v2 != null) {
                v2.close();
            }

            if (v3 != null) {
                v3.close();
            }
        }
    }

    @Nullable
    public static String a(String arg4) {
        String v0_2;
        if (arg4 != null && !arg4.isEmpty()) {
            boolean v2 = URLUtil.isHttpsUrl(arg4);
            int v0 = arg4.indexOf(47, 8);
            String v1 = v0 != -1 ? arg4.substring(0, v0) : arg4;
            try {
                v0_2 = new URI(v1).getHost();
            } catch (URISyntaxException v0_1) {
                v0_1.printStackTrace();
                v0_2 = null;
            }

            if (v0_2 != null && !v0_2.isEmpty()) {
                if (v2) {
                    v0_2 = "https://" + v0_2;
                } else if (v0_2.startsWith("www.")) {
                    v0_2 = v0_2.substring(4);
                } else {
                }

                return v0_2;
            }

            v0_2 = v1;
        } else {
            v0_2 = "";
        }

        return v0_2;
    }

    public static Pair<String,String> parseUrl(String url, Map queryMap) throws IOException {
        if (android.support.design.a.isTextEmpty(url)) {
            throw new IOException("parseUrl url is null !!!");
        }

        try {
            Uri uri = Uri.parse(url);
            StringBuilder stringBuilder = new StringBuilder();
            String scheme = uri.getScheme();
            String host = uri.getHost();
            int port = uri.getPort();
            if (host != null) {
                if (scheme != null) {
                    stringBuilder.append(scheme);
                    stringBuilder.append("://");
                }
                stringBuilder.append(host);
                if (port > 0) {
                    stringBuilder.append(':');
                    stringBuilder.append(port);
                }
            }
            String website = stringBuilder.toString();
            String path = uri.getPath();
            String encodedQuery = uri.getEncodedQuery();
            if (encodedQuery != null) {
                String[] queryParams = encodedQuery.split("&");
                int length = queryParams.length;
                for (int i = 0; i < length; i++) {
                    String param = queryParams[i];
                    int index = param.indexOf("=");
                    if (index >= 0) {
                        queryMap.put(URLDecoder.decode(param.substring(0, index), "UTF-8"), URLDecoder.decode(param.substring(index + 1), "UTF-8"));
                    } else {
                        queryMap.put(URLDecoder.decode(param, "UTF-8"), "");
                    }
                }
            }
            return new Pair(website, path);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            throw new IOException("parseUrl url is fail !!!");
        }
    }

    public static MonitorNetUtil$NetworkType b(Context arg2) {
        MonitorNetUtil$NetworkType v0_2;
        try {
            NetworkInfo v0_1 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_1 == null || !v0_1.isAvailable()) {
                v0_2 = MonitorNetUtil$NetworkType.NONE;
                return v0_2;
            }

            int v0_3 = v0_1.getType();
            if (1 == v0_3) {
                return MonitorNetUtil$NetworkType.WIFI;
            }

            if (v0_3 != 0) {
                return MonitorNetUtil$NetworkType.MOBILE;
            }

            switch (((TelephonyManager) arg2.getSystemService(Context.TELEPHONY_SERVICE)).getNetworkType()) {
                case 13: {
                    return MonitorNetUtil$NetworkType.MOBILE_4G;
                }
                case 3:
                case 5:
                case 6:
                case 8:
                case 9:
                case 10:
                case 12:
                case 14:
                case 15: {
                    return MonitorNetUtil$NetworkType.MOBILE_3G;
                }
            }
            return MonitorNetUtil$NetworkType.MOBILE;


        } catch (Throwable v0) {
            v0_2 = MonitorNetUtil$NetworkType.MOBILE;
        }

        return v0_2;
    }

    public static byte[] a(String arg7, byte[] arg8, MonitorNetUtil$CompressType arg9, String arg10, boolean arg11) throws Throwable {
        String v0_3;
        String v2_2;
        byte[] v1_3;
        String v3_1;
        int v4 = 128;
        byte[] v0 = null;
        int v3 = 8192;
        if (arg7 != null) {
            if (arg8 == null) {
                arg8 = new byte[0];
            }

            int v1 = arg8.length;
            if (MonitorNetUtil$CompressType.GZIP != arg9 || v1 <= v4) {
                if (MonitorNetUtil$CompressType.DEFLATER == arg9 && v1 > v4) {
                    ByteArrayOutputStream v0_2 = new ByteArrayOutputStream(v3);
                    Deflater v1_4 = new Deflater();
                    v1_4.setInput(arg8);
                    v1_4.finish();
                    byte[] v2_1 = new byte[v3];
                    while (!v1_4.finished()) {
                        v0_2.write(v2_1, 0, v1_4.deflate(v2_1));
                    }

                    v1_4.end();
                    v3_1 = "deflate";
                    v1_3 = v0_2.toByteArray();
                } else {
                    v3_1 = ((String) null);
                    v1_3 = arg8;
                }
            } else {
                ByteArrayOutputStream v1_1 = new ByteArrayOutputStream(v3);
                GZIPOutputStream v2 = new GZIPOutputStream(((OutputStream) v1_1));
                try {
                    v2.write(arg8);
                } catch (Throwable v0_1) {
                    v2.close();
                } finally {
                    v2.close();
                }

                v3_1 = "gzip";
                v1_3 = v1_1.toByteArray();
            }

            if (arg11) {
                v0 = TTEncryptUtils.encryptBytes(v1_3, v1_3.length);
                if (v0 != null) {
                    v2_2 = "application/octet-stream;tt-data=a";
                    v1_3 = v0;
                    v0_3 = arg7 + "&tt_data=a";
                } else {
                    v2_2 = arg10;
                    v0_3 = arg7;
                }

                v0 = FileUtils.a(v0_3, v1_3, v2_2, v3_1, "POST", true, true);
            } else {
                v0 = FileUtils.a(arg7, v1_3, arg10, v3_1, "POST", true, false);
            }
        }

        return v0;
    }

    public static boolean a(Context arg2) {
        try {
            NetworkInfo v0_1 = ((ConnectivityManager) arg2.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
            if (v0_1 != null) {
                if (!v0_1.isConnected()) {
                    return false;
                }

                return true;
            }

            return false;
        } catch (Exception v0) {
            boolean v0_2 = false;
            return v0_2;
        }
    }

    public static String a(NetworkUtils$NetworkType arg3) {
        String v0 = "";
        try {
            switch (b.a[arg3.ordinal()]) {
                case 1: {
                    return "wifi";
                }
                case 2: {
                    return "2g";
                }
                case 3: {
                    return "3g";
                }
                case 4: {
                    return "4g";
                }
                case 5: {
                    return "mobile";
                }
            }

            return v0;
        } catch (Exception v1) {
        }

        return v0;
    }


}
